package xyz.ibenben.zhongdian.system.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.ibenben.zhongdian.common.annotation.SystemControllerLog;
import xyz.ibenben.zhongdian.common.constants.Constants;
import xyz.ibenben.zhongdian.common.exception.ExceptionEnum;
import xyz.ibenben.zhongdian.common.exception.MyException;
import xyz.ibenben.zhongdian.common.util.HttpServletResponseUtil;
import xyz.ibenben.zhongdian.system.entity.HouseImage;
import xyz.ibenben.zhongdian.system.form.HouseImageForm;
import xyz.ibenben.zhongdian.system.service.HouseImageService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 房屋图像控制类
 * 提供了一些基本的服务，如初始化、获取房屋图像列表等方法。
 * spring mvc思想 通过controller来调用service里的方法，
 * service里的方法再通过调用Dao来实现对数据的操作
 * 返回值统统是String类型的返回页面有统一的安排方便代码提取
 *
 * @author chenjian
 * @since 2017年9月27日上午10:57:28
 */
@Controller
@RequestMapping("/houseImage")
public class HouseImageController extends BaseController{
	/* 实体名称 */
	private static final String ENTITYNAME = "houseImage";
	/* 上传页面 */
	private static final String UPLOADPAGE = ENTITYNAME + "/upload";

	private final HouseImageService houseImageService;
	
	private List<MultipartFile> files = new ArrayList<>();

	@Autowired
	public HouseImageController(HouseImageService houseImageService) {
		this.houseImageService = houseImageService;
	}

	/**
	 * 获取房屋图像信息列表
	 * @param map 参数
	 * @param houseImageForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/getall")
	@SystemControllerLog(description = "获取房屋图像信息列表")
	public String getall(Model map, @ModelAttribute HouseImageForm houseImageForm){
		HouseImageForm newHouseImageForm = new HouseImageForm();
		if(houseImageForm != null){
			newHouseImageForm = houseImageForm;
		}
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newHouseImageForm);
		Page<HouseImage> page = PageHelper.startPage(newHouseImageForm.getPageIndex(), newHouseImageForm.getPageSize());
		List<HouseImage> list = houseImageService.selectByExample(newHouseImageForm.buildSearchCommond());
		map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), list);
		map.addAttribute(Constants.PI, page.toPageInfo());
		return String.format(Constants.LISTPAGE, ENTITYNAME);
	}

	/**
	 * 删除房屋图像信息
	 * @param imageId 主键
	 * @param request 请求
	 * @return 页面
	 */
	@ResponseBody
	@RequestMapping(value = "/{imageId}/delete")
	@SystemControllerLog(description = "删除房屋图像信息")
	public String delete(@PathVariable Long imageId, HttpServletRequest request) {
		HouseImage image = houseImageService.selectByKey(imageId);
		houseImageService.delete(image, request);
		return Constants.OK;
	}

	/**
	 * 打开房屋图像上传页面
	 * @param map 参数
	 * @param path 路径
	 * @param id 主键
	 * @return 页面
	 */
	@RequestMapping(value="/{path}/{id}/upload", method= RequestMethod.GET)
	@SystemControllerLog(description = "打开房屋图像上传页面")
	public String openUploadPage(Model map, @PathVariable String path, @PathVariable Long id){
		map.addAttribute(Constants.ID, id);
		map.addAttribute("path", path);
		map.addAttribute("list", houseImageService.findByHouseId(id));
		return UPLOADPAGE;
	}

	/**
	 * 异步上传房屋图像记录
	 * @param file 文件
	 * @return 页面
	 */
	@RequestMapping(value="/upload", method= RequestMethod.POST)
	@ResponseBody
	@SystemControllerLog(description = "异步上传房屋图像记录")
	public String ajaxPostUploadImage(@RequestParam("file") MultipartFile file){
		String result = Constants.ERROR;
		try {
			if(file.getInputStream() != null){
                files.add(file);
                result = Constants.OK;
            }
		} catch (IOException e) {
			throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
		}
		return result;
	}

	/**
	 * 提交上传房屋图像记录
	 * @param map 参数
	 * @param path 路径
	 * @param id 主键
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value="/{path}/{id}/uploadsubmit", method= RequestMethod.GET)
	@SystemControllerLog(description = "提交上传房屋图像记录")
	public String postUploadImage(Model map,@PathVariable("path") String path, @PathVariable Long id, HttpServletRequest request){
		if(files != null && !files.isEmpty()){
			List<HouseImage> list = houseImageService.findByHouseId(id);
			int listSize = list == null ? 0 : list.size();
			
			if(listSize < 6 && listSize + files.size() <= 6){
				houseImageService.insert(id, files, request);
			}else{
				files.clear();
				map.addAttribute("list", list);
				return HttpServletResponseUtil.processModelAndView(map, "最多可以上传6张图片", UPLOADPAGE);
			}
			files.clear();
		}
		return "redirect:/" + path + "/getall";
	}
	
}
